Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not retry past formats when loading all formats for the first time #6902

Merged
merged 1 commit into from
Jan 21, 2023

Conversation

radarhere
Copy link
Member

When opening an image, Pillow loops through the different possible formats.

Pillow/src/PIL/Image.py

Lines 3242 to 3244 in 0b53853

def _open_core(fp, filename, prefix, formats):
for i in formats:
i = i.upper()

If I add a print statement to see which formats it checks,

    def _open_core(fp, filename, prefix, formats):
        for i in formats:
            print(i)
            i = i.upper()

and then run

from PIL import Image
Image.open("Tests/images/hopper.spider")

I get

BMP
DIB
GIF
JPEG
PPM
PNG
BMP
DIB
GIF
JPEG
PPM
PNG
BLP
BUFR
CUR
PCX
DCX
...

The first set of formats are checked twice - BMP, DIB, GIF, JPEG, PPM and PNG.

This is because after the preinit formats fail to load the SPIDER image, Pillow initialises the rest of the formats, and then just checks all the formats again, even the preinit ones that already failed.

Pillow/src/PIL/Image.py

Lines 3268 to 3272 in 43bb035

im = _open_core(fp, filename, prefix, formats)
if im is None:
if init():
im = _open_core(fp, filename, prefix, formats)

So this PR excludes the already checked formats the second time.

@hugovk hugovk merged commit 6d09f33 into python-pillow:main Jan 21, 2023
@radarhere radarhere deleted the open branch January 21, 2023 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants